﻿//page load
function initialPage() {
    var id = $('#workflowid').val();
    workflowid = id;//避免 在ie内找不到方法定义的bug, 在声明全局变量时 应该先声明一个局部变量 然后复制给全局变量
    initialControls();
    initialDefinitions();
    initialWorkflow();
    $('.ws-fields').sortable();
}
function initialWorkflow() {
    $.ajax({
        url: "/Workflow/DesignJson",
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: { targetid: workflowid },
        success: function (result) {
            if (result) {
                setFieldsData(result.Fields);
                setConditionData(result.Conditions);
                setParticipantData(result.Participants);
                setActivityData(result.Activities);
                setActivityResultData(result.ActivityResults)
            }
        }
    });
}

function initialDefinitions() {
    var $fieldli = $('<li class="dc-field unsave" style="float:left; height:45px;"><div class="text-display" style="line-height:20px;text-align:center;"></div></li>');
    $.registerListItem('ws-fields', $fieldli);
    $.registerListItem('ws-participants', $('<li class="unsave workplace-ul-item"  saved="false"><div class="display-text" style="line-height:20px;text-align:center;"></div></li>'));
    $.registerListItem('ws-conditions', $('<li class="unsave workplace-ul-item"  saved="false"><div class="display-text" style="line-height:20px;text-align:center;"></div></li>'));
    $.registerListItem('ws-containers', $('<li class="ws-containeritem unsave"><ul class="ws-activities"></ul></li>'));
    $.registerListItem('ws-activities', $('<li class="unsave workplace-ul-item"><div class="display-text"></div></li>'));
    $.registerListItem('ws-activityresults', $('<li class="unsave workplace-ul-item"><div class="display-text"></div></li>'));
    $.registerListItem('pre-form', $('<li class="preview-field"></li>'));
}

function getWorkflowData() {
    var fs = getFieldsData();
    var cs = getCondtionsData();
    var ps = getParticipantsData();
    var as = getActivitiesData();
    var ars = getActivityResultsData();
    var w = { Fields: fs, Conditions: cs, ID: workflowid, Version: 1, Participants: ps, Activities:as,ActivityResults:ars, ModifiedTime: new Date()};

    for (var i = 0; i < ps.length; i++) {
        for (var j = 0; j < ps[i].Employees.length; j++) {
            ps[i].Employees[j].Department = null;
        }
    }
    for (var i = 0; i < as.length; i++) {
        for (var j = 0; j < as[i].Participants.length; j++) {
            for (var k = 0; k < as[i].Participants[j].Employees.length; k++)
            as[i].Participants[j].Employees[k].Department = null;
        }
    }
    for (var i = 0; i < ars.length; i++) {
        ars[i].Activity.Participants = null;
    }
    w = JSON.stringify(w);
    return w;
}

function initialControls() {
    $('.dc_process_save').click(function () {
        p = getWorkflowData();

        $.ajax({
            url: "/Workflow/SaveProcess",
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: p,
            success: function (result) {
            }
        });
    });
}

//public functions
function getTreeviewSelectedItemText($treeview, e) {
    var tv = $treeview.data('tTreeView');
    var nodetext = tv.getItemText(e.item);
    return nodetext;
}

function setDetailToWindowEditor($target, intial, $w) {
    var $screen = $(document);
    $w.css('left', $screen.width() / 2 - $w.width() / 2).css('top', '150px');
    $w.data('tWindow').content(intial).title('编辑框').open();
}

function onConditionNodeDropped(e) {
    if ($(e.destinationItem).attr('elementtype') == 'condition-single') {
        e.preventDefault();
        alert("单条件不能添加子项，请用或运算或者并运算代替");
    }
}